<html>
<head>
    <title>打开小程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
    <script>
        window.onerror = e => {
            console.error(e)
            alert('发生错误' + e)
        }
    </script>
    <!-- weui 样式 -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css"></link>
    <!-- 调试用的移动端 console -->
    <!-- <script src="https://cdn.jsdelivr.net/npm/eruda"></script> -->
    <!-- <script>eruda.init();</script> -->
    <!-- 公众号 JSSDK -->
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <!-- 云开发 Web SDK -->
    <script src="https://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!--    <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>-->
<!--    <script>-->
<!--        // VConsole 默认会挂载到 `window.VConsole` 上-->
<!--        var vConsole = new window.VConsole();-->
<!--    </script>-->
    <script>
        function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
            var context = "";
            if (r != null)
                context = decodeURIComponent(r[2]);
            reg = null;
            r = null;
            return context == null || context == "" || context == "undefined" ? "" : context;
        }
        function docReady(fn) {
            if (document.readyState === 'complete' || document.readyState === 'interactive') {
                fn()
            } else {
                document.addEventListener('DOMContentLoaded', fn);
            }
        }
        function toPay() {
            let baseUrl = GetQueryString('baseUrl')
            let order_no = GetQueryString('order_no')
            console.log(order_no,'order_no')
            // let requestUrl = `${baseUrl}/api/payment/wxLitePay?orderNo=` + order_no
            let requestUrl = 'https://pay.xth66.com/pay/api/payment/wxLitePay?orderNo=' + order_no
            $.ajax({
                url: requestUrl,
                type: 'GET',
                dataType: "JSON",
                contentType: "application/json",
                success: function (data) {
                    if (data.code == 0) {
                        initSdk(data.data,order_no)
                    }else {

                    }
                },
                error:function(){

                }
            });
        }
        docReady(async function() {
            toPay()
        })
        const initSdk = async (res,order_no)=>{
            var ua = navigator.userAgent.toLowerCase()
            var isWXWork = ua.match(/wxwork/i) == 'wxwork'
            var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
            var isMobile = false
            var isDesktop = false
            if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
                isMobile = true
            } else {
                isDesktop = true
            }
            if (isWeixin) {
                var containerEl = document.getElementById('wechat-web-container')
                containerEl.classList.remove('hidden')
                containerEl.classList.add('full', 'wechat-web-container')
                var launchBtn = document.getElementById('launch-btn')
                launchBtn.addEventListener('ready', function (e) {
                    let myPath = 'pages/payCs/index?order_no=' + order_no
                    launchBtn.setAttribute('path',myPath)
                    console.log('开放标签 ready')
                })
                launchBtn.addEventListener('launch', function (e) {
                    console.log('开放标签 success')
                })
                launchBtn.addEventListener('error', function (e) {
                    console.log('开放标签 fail', e.detail)
                })

                wx.config({
                    debug: false, // 调试时可开启
                    appId: res.appId, // <!-- replace -->
                    timestamp: res.timestamp, // 必填，填任意数字即可
                    nonceStr: res.nonceStr, // 必填，填任意非空字符串即可
                    signature: res.sign, // 必填，填任意非空字符串即可
                    jsApiList: ['chooseImage'], // 必填，随意一个接口即可
                    openTagList:['wx-open-launch-weapp'], // 填入打开小程序的开放标签名
                })
            } else if (isDesktop) {
                // 在 pc 上则给提示引导到手机端打开
                var containerEl = document.getElementById('desktop-web-container')
                containerEl.classList.remove('hidden')
                containerEl.classList.add('full', 'desktop-web-container')
            }  else {
                var containerEl = document.getElementById('public-web-container')
                containerEl.classList.remove('hidden')
                containerEl.classList.add('full', 'public-web-container')
                var buttonEl = document.getElementById('public-web-jump-button')
                var buttonLoadingEl = document.getElementById('public-web-jump-button-loading')
                try {

                } catch (e) {
                    buttonEl.classList.remove('weui-btn_loading')
                    buttonLoadingEl.classList.add('hidden')
                    throw e
                }
            }
        }
    </script>
    <style>
        .hidden {
            display: none;
        }

        .full {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
        }

        .public-web-container {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .public-web-container p {
            position: absolute;
            top: 40%;
        }

        .public-web-container a {
            position: absolute;
            bottom: 40%;
        }

        .wechat-web-container {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .wechat-web-container p {
            position: absolute;
            top: 40%;
        }

        .wechat-web-container wx-open-launch-weapp {
            position: absolute;
            bottom: 40%;
            left: 0;
            right: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .desktop-web-container {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .desktop-web-container p {
            position: absolute;
            top: 40%;
        }
    </style>
</head>
<body>
<div class="page full">
    <div id="public-web-container" class="hidden">
        <p class="">请用微信扫一扫打开</p> <!-- replace -->
    </div>
    <div id="wechat-web-container" class="hidden">
        <!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
        <wx-open-launch-weapp id="launch-btn" username="gh_ba46d7dc518a"> <!-- replace -->
            <template>
                <button style="width: 200px; height: 45px; text-align: center; font-size: 17px; display: block; margin: 0 auto; padding: 8px 24px; border: none; border-radius: 4px; background-color: #07c160; color:#fff;">打开小程序</button>
            </template>
        </wx-open-launch-weapp>
    </div>
    <div id="desktop-web-container" class="hidden">
        <p class="">请在手机打开网页链接</p>
    </div>
</div>
</body>
</html>